public class Array<E> {
    private E[] data;
    private int size;

    /**
     * @param capacity
     */
    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    public Array() {
        this(10);
    }

    public Array(E[] arr) {
        data = (E[]) new Object[arr.length];
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
            size = arr.length;
        }
    }

    public int getSize() {
        return size;
    }

    public int getCapacity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 向数组尾部添加元素
     *
     * @param e
     */
    public void addLast(E e) {
        add(size, e);
    }

    /**
     * 向数组头部添加元素
     *
     * @param e
     */
    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 向任意位置添加元素  大于index位置的每个元素都向后移动
     *
     * @param index
     * @param e
     */
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed");
        }
        if (size == data.length) {
            resize(2 * data.length);
        }
        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        size++;
    }

    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0; i < newData.length; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }

    /**
     * 删除任意位置的元素 并返回被删除的元素
     * 被删除的元素后的每个元素向前移动一个
     *
     * @return
     */
    public E remove(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("index error");
        }
        E ret = data[index];
        for (int i = index + 1; i < size; i++) {
            data[i - 1] = data[i];
        }
        size--;
        data[size] = null;
        if (size == data.length / 4 && data.length / 2 != 0) {
            resize(data.length / 2);
        }
        return ret;
    }

    public E removeFirst() {
        return remove(0);
    }

    public E removeLast() {
        return remove(size - 1);
    }

    public void removeElement(E e) {
        if (find(e) != -1) {
            remove(find(e));
        }
    }

    /**
     * 修改index位置元素的值
     *
     * @param index
     * @param e
     */
    public void set(int index, E e) {
        data[index] = e;
    }


    /**
     * 查找数组中是否包含某一元素
     *
     * @param e
     * @return
     */
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return true;
            }
        }
        return false;
    }

    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("index out of bound");
        }
        return data[index];
    }


    /**
     * 查找数组中的某个元素并返回索引
     *
     * @param e
     * @return
     */
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return i;
            }
        }
        return -1;
    }

    public void swap(int i, int j) {
        if (i < 0 || i >= size || j < 0 || j <= size) {
            throw new IllegalArgumentException("index out of bound");
        }
        E temp = data[i];
        data[i] = data[j];
        data[j] = temp;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Array size:" + this.size + ", capacity:" + this.getCapacity() + " ");
        builder.append("[ ");
        for (int i = 0; i < size; i++) {
            builder.append(data[i] + " ");
        }
        builder.append("]");
        return builder.toString();
    }
}
